<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'
import {ref} from 'vue'
import { computed } from 'vue'
import { onMounted } from 'vue'// 引入onMounted,用于localStorage

const inputValue = ref('')//输入框绑定的变量
interface TodoItem {//声明一个TodoItem接口
  text: string
  detail: string // 新增详情字段
  iscompleted: boolean
  isEditing?: boolean // 添加编辑状态
  editText?: string // 编辑时的临时文本
  editDetail?: string // 编辑时的临时详情
}
const items = ref<TodoItem[]>([])//声明一个items数组,用于存储任务

// 详情模态框相关状态
const showDetailModal = ref(false)
const currentItem = ref<TodoItem | null>(null)

//获取localStorage中的数据 
onMounted(() => {
    const savedItems = localStorage.getItem('todostorage');
    if (savedItems) {
        const parsedItems = JSON.parse(savedItems);
        // 确保每个项目都有正确的类型和属性
        items.value = parsedItems.map((item: any) => ({
            text: item.text || '',
            detail: item.detail || '', // 读取详情
            iscompleted: item.iscompleted || false,
            isEditing: false,
            editText: item.text || '',
            editDetail: item.detail || ''
        }));
    }
});

//存储items数据到localStorage
function saveToLocalStorage() {
    const todoList=JSON.stringify(items.value.map(item => ({
        text: item.text,
        detail: item.detail, // 保存详情
        iscompleted: item.iscompleted
    })));
    localStorage.setItem('todostorage',todoList);//保存数据
}

//示例
const a=['a','b','c'];
const b=JSON.stringify(a);//将a的数据转换为JISON格式的字符串
localStorage.setItem('test',b);//括号内，test为存储的名称，b为要存储在test里面的数据
//示例

const completed = computed(() => {
  // 计算属性，返回已完成的任务
  return items.value.filter(items => items.iscompleted)
})

const uncompleted = computed(() => {
  // 计算属性，返回未完成的任务
  return items.value.filter(items => !items.iscompleted)
})

function additems() {// 添加任务
  // 只有当输入不为空时才添加
  if (inputValue.value.trim()) {
      items.value.push({
      text: inputValue.value,
      detail: '', // 初始化详情为空
      iscompleted: false,
      isEditing: false,
      editText: inputValue.value,
      editDetail: ''
    })
    // 添加后清空输入框
    inputValue.value = '';
    saveToLocalStorage();
  }
}

function del(item: TodoItem){
  const index = items.value.findIndex(i => i === item);
  if (index !== -1) {
    items.value.splice(index, 1);
    saveToLocalStorage();
  }
}

// 查看详情功能
function showDetail(item: TodoItem) {
  currentItem.value = item;
  showDetailModal.value = true;
}

// 关闭详情模态框
function closeDetailModal() {
  showDetailModal.value = false;
  currentItem.value = null;
}

// 编辑功能
function startEdit(item: TodoItem) {
  item.isEditing = true;
  item.editText = item.text;
  item.editDetail = item.detail;
}

function saveEdit(item: TodoItem) {
  if (item.editText && item.editText.trim()) {
    item.text = item.editText.trim();
    item.detail = item.editDetail || ''; // 保存详情
    item.isEditing = false;
    saveToLocalStorage();
  }
}

function cancelEdit(item: TodoItem) {
  item.isEditing = false;
  item.editText = item.text;
  item.editDetail = item.detail;
}

// 切换任务完成状态
function toggleComplete(item: TodoItem) {
  item.iscompleted = !item.iscompleted;
  saveToLocalStorage();
}


</script>

<template>
	<div class="container">
    <div class="container">
      
    </div>
    <header>
      <h1>待办事项</h1>
    </header>

    <div class="input-area">
      <input type="text" class="task-input" v-model="inputValue" placeholder="添加新任务..." v-on:keyup.enter="additems()">
      <button class="add-btn" @click="additems()">添加</button>
    </div>

    <div class="task-list">
      <!-- 待完成任务 -->
      <div v-if="uncompleted.length > 0">
        <div class="section-title">待完成 ({{ uncompleted.length }})</div>
        <ul class="todo" id="todo">
          <li v-for="item in uncompleted" :key="item.text">  
            <!-- 编辑模式 -->
            <div v-if="item.isEditing" class="edit-mode">
              <div class="edit-inputs">
                <input 
                  type="text" 
                  v-model="item.editText" 
                  class="edit-input"
                  placeholder="任务标题"
                  @keyup.enter="saveEdit(item)"
                  @keyup.esc="cancelEdit(item)"
                  v-focus
                >
                <textarea 
                  v-model="item.editDetail" 
                  class="edit-textarea"
                  placeholder="任务详情"
                  rows="3"
                ></textarea>
              </div>
              <div class="edit-buttons">
                <button class="save-btn" @click="saveEdit(item)" title="保存">保存</button>
                <button class="cancel-btn" @click="cancelEdit(item)" title="取消">取消</button>
              </div>
            </div>
            
            <!-- 显示模式 -->
            <div v-else class="display-mode">
              <span class="text">{{ item.text }}</span>
              <div class="buttons">
                <button class="detail" @click="showDetail(item)" title="查看详情">
                  <svg viewBox="0 0 16 16" fill="currentColor">
                    <path d="M8 16A8 8 0 1 1 8 0a8 8 0 0 1 0 16zM8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1zm0 9.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2zM8 6a1 1 0 0 1-1-1V3a1 1 0 0 1 2 0v2a1 1 0 0 1-1 1z"/>
                  </svg>
                </button>

                <button class="complete" @click="toggleComplete(item)" title="标记为完成">
                  <svg viewBox="0 0 16 16" fill="currentColor">
                    <path d="M13.5 2L6 9.5 2.5 6 1 7.5l5 5 8-8L13.5 2z"/>
                  </svg>
                </button>

                <button class="edit" @click="startEdit(item)" title="编辑任务">
                  <svg viewBox="0 0 16 16" fill="currentColor">
                    <path d="M11.013 1.427a1.75 1.75 0 0 1 2.474 0l1.086 1.086a1.75 1.75 0 0 1 0 2.474l-8.61 8.61a1.75 1.75 0 0 1-.624.42l-3.58 1.48a.75.75 0 0 1-.958-.958l1.48-3.58a1.75 1.75 0 0 1 .42-.624l8.61-8.61Zm1.414 1.06a.25.25 0 0 0-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 0 0 0-.354l-1.086-1.086ZM11.189 6.25 9.75 4.81l-6.286 6.287a.25.25 0 0 0-.064.108l-.558 1.953 1.953-.558a.25.25 0 0 0 .108-.064l6.286-6.286Z"/>
                  </svg>
                </button>

                <button class="remove" @click="del(item)" title="删除任务">
                  <svg viewBox="0 0 16 16" fill="currentColor">
                    <path d="M14 3h-4V2c0-.6-.4-1-1-1H7c-.6 0-1 .4-1 1v1H2c-.6 0-1 .4-1 1s.4 1 1 1h1v9c0 .6.4 1 1 1h8c.6 0 1-.4 1-1V5h1c.6 0 1-.4 1-1s-.4-1-1-1zM6 2h4v2H6V2zm6 12H4V5h8v9z"/>
                  </svg>
                </button>
              </div>
            </div>
          </li>
        </ul>
      </div>
      
      <!-- 已完成任务 -->
      <div v-if="completed.length > 0">
        <div class="section-title">已完成 ({{ completed.length }})</div>
        <ul class="todo" id="completed">
          <li v-for="item in completed" :key="item.text">  
            <!-- 编辑模式 -->
            <div v-if="item.isEditing" class="edit-mode">
              <div class="edit-inputs">
                <input 
                  type="text" 
                  v-model="item.editText" 
                  class="edit-input"
                  placeholder="任务标题"
                  @keyup.enter="saveEdit(item)"
                  @keyup.esc="cancelEdit(item)"
                  v-focus
                >
                <textarea 
                  v-model="item.editDetail" 
                  class="edit-textarea"
                  placeholder="任务详情"
                  rows="3"
                ></textarea>
              </div>
              <div class="edit-buttons">
                <button class="save-btn" @click="saveEdit(item)" title="保存">保存</button>
                <button class="cancel-btn" @click="cancelEdit(item)" title="取消">取消</button>
              </div>
            </div>
            
            <!-- 显示模式 -->
            <div v-else class="display-mode">
              <span class="text">{{ item.text }}</span>
              <div class="buttons">
                <button class="detail" @click="showDetail(item)" title="查看详情">
                  <svg viewBox="0 0 16 16" fill="currentColor">
                    <path d="M8 16A8 8 0 1 1 8 0a8 8 0 0 1 0 16zM8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1zm0 9.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2zM8 6a1 1 0 0 1-1-1V3a1 1 0 0 1 2 0v2a1 1 0 0 1-1 1z"/>
                  </svg>
                </button>

                <button class="complete" @click="toggleComplete(item)" title="标记为未完成">
                  <svg viewBox="0 0 16 16" fill="currentColor">
                    <path d="M13.5 2L6 9.5 2.5 6 1 7.5l5 5 8-8L13.5 2z"/>
                  </svg>
                </button>

                <button class="edit" @click="startEdit(item)" title="编辑任务">
                  <svg viewBox="0 0 16 16" fill="currentColor">
                    <path d="M11.013 1.427a1.75 1.75 0 0 1 2.474 0l1.086 1.086a1.75 1.75 0 0 1 0 2.474l-8.61 8.61a1.75 1.75 0 0 1-.624.42l-3.58 1.48a.75.75 0 0 1-.958-.958l1.48-3.58a1.75 1.75 0 0 1 .42-.624l8.61-8.61Zm1.414 1.06a.25.25 0 0 0-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 0 0 0-.354l-1.086-1.086ZM11.189 6.25 9.75 4.81l-6.286 6.287a.25.25 0 0 0-.064.108l-.558 1.953 1.953-.558a.25.25 0 0 0 .108-.064l6.286-6.286Z"/>
                  </svg>
                </button>

                <button class="remove" @click="del(item)" title="删除任务">
                  <svg viewBox="0 0 16 16" fill="currentColor">
                    <path d="M14 3h-4V2c0-.6-.4-1-1-1H7c-.6 0-1 .4-1 1v1H2c-.6 0-1 .4-1 1s.4 1 1 1h1v9c0 .6.4 1 1 1h8c.6 0 1-.4 1-1V5h1c.6 0 1-.4 1-1s-.4-1-1-1zM6 2h4v2H6V2zm6 12H4V5h8v9z"/>
                  </svg>
                </button>
              </div>
            </div>
          </li>
        </ul>
      </div>
      
      <div class="empty-state" v-if="items.length === 0">
        <p>暂无任务，请添加新任务</p>
      </div>
    </div>

    <!-- 详情模态框 -->
    <div v-if="showDetailModal" class="modal-overlay" @click="closeDetailModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h2>任务详情</h2>
          <button class="close-btn" @click="closeDetailModal">×</button>
        </div>
        <div class="modal-body">
          <div class="detail-section">
            <h3>标题</h3>
            <p>{{ currentItem?.text }}</p>
          </div>
          <div class="detail-section">
            <h3>详情</h3>
            <p v-if="currentItem?.detail">{{ currentItem.detail }}</p>
            <p v-else class="no-detail">暂无详情</p>
          </div>
          <div class="detail-section">
            <h3>状态</h3>
            <p :class="currentItem?.iscompleted ? 'completed' : 'pending'">
              {{ currentItem?.iscompleted ? '已完成' : '待完成' }}
            </p>
          </div>
        </div>
        <div class="modal-footer">
          <button class="edit-btn" @click="currentItem && startEdit(currentItem); closeDetailModal()">编辑</button>
          <button class="close-modal-btn" @click="closeDetailModal">关闭</button>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
@charset "UTF-8";
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

body {
  background: linear-gradient(135deg, #ffffff 0%, #ffffff 100%);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 20px;
  color: #333;
}

#app {
  width: 100%;
  display: flex;
  justify-content: center;
}

.container {
  width: 100%;
  max-width: 600px;
  background: rgb(255, 255, 255);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(82, 81, 81, 0.1);
  overflow: hidden;
  position: relative;
}

header {
  background: #5f7a7c;
  color: white;
  padding: 20px;
  text-align: center;
}

h1 {
  font-weight: 600;
  font-size: 1.8rem;
  margin-bottom: 5px;
}

.input-area {
  padding: 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
}

.task-input {
  flex: 1;
  padding: 14px;
  border: 2px solid #ddd;
  border-radius: 8px 0 0 8px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.3s;
}

.task-input:focus {
  border-color: #5f7a7c;
}

.add-btn {
  background: #5f7a7c;
  color: white;
  border: none;
  padding: 0 25px;
  border-radius: 0 8px 8px 0;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.3s;
}

.add-btn:hover {
  background: #4a6062;
}

.task-list {
  padding: 10px 0;
  min-height: 200px;
  max-height: 400px;
  overflow-y: auto;
}

.section-title {
  padding: 10px 20px;
  color: #5f7a7c;
  font-weight: 600;
  border-bottom: 1px solid #eee;
  background-color: #f9f9f9;
}

ul.todo {
  width: 100%;
  padding: 0 20px;
}

ul.todo li {
  width: 100%;
  min-height: 50px;
  font-size: 14px;
  font-weight: 500;
  color: #444;
  line-height: 22px;
  background: #fff;
  border-radius: 8px;
  position: relative;
  box-shadow: 0px 1px 3px rgba(44, 62, 80, 0.10);
  margin: 0 0 10px 0;
  padding: 14px 200px 14px 14px; /* 增加右侧空间以容纳更多按钮 */
  word-break: break-word;
  display: flex;
  align-items: center;
  transition: all 0.2s ease;
}

ul.todo li:hover {
  box-shadow: 0px 3px 6px rgba(44, 62, 80, 0.15);
  transform: translateY(-1px);
}

ul.todo li:last-of-type {
  margin: 0;
}

/* 编辑模式样式 */
.edit-mode {
  display: flex;
  align-items: flex-start;
  width: 100%;
  justify-content: space-between;
  flex-direction: column;
}

.edit-inputs {
  width: 100%;
  margin-bottom: 10px;
}

.edit-input {
  width: 100%;
  padding: 8px;
  border: 2px solid #5f7a7c;
  border-radius: 4px;
  font-size: 14px;
  outline: none;
  margin-bottom: 8px;
}

.edit-textarea {
  width: 100%;
  padding: 8px;
  border: 2px solid #5f7a7c;
  border-radius: 4px;
  font-size: 14px;
  outline: none;
  resize: vertical;
  min-height: 60px;
  font-family: inherit;
}

.edit-buttons {
  display: flex;
  align-self: flex-end;
}

.save-btn, .cancel-btn {
  padding: 6px 12px;
  margin-left: 5px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
}

.save-btn {
  background: #25b99a;
  color: white;
}

.save-btn:hover {
  background: #1a8c72;
}

.cancel-btn {
  background: #c0cecb;
  color: #333;
}

.cancel-btn:hover {
  background: #a0b0ad;
}

ul.todo li .buttons {
  width: 200px; /* 增加宽度以容纳更多按钮 */
  height: 50px;
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
}

ul.todo li .buttons button {
  width: 50px;
  height: 50px;
  background: none;
  border: 0px;
  box-shadow: none;
  outline: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-appearance: none;
  -moz-appearance: none;
}

ul.todo li .buttons button:last-of-type:before {
  content: '';
  width: 1px;
  height: 30px;
  background: #edf0f1;
  position: absolute;
  top: 10px;
  left: 0;
}

ul.todo li .buttons button svg {
  width: 20px;
  height: 20px;
}

ul.todo li .buttons button.detail svg {
  color: #5f7a7c;
  transition: color 0.2s ease;
}

ul.todo li .buttons button.detail:hover svg {
  color: #4a6062;
}

ul.todo li .buttons button.complete svg {
  color: #25b99a;
  transition: color 0.2s ease;
}

ul.todo li .buttons button.complete:hover svg {
  color: #1a8c72;
}

ul.todo li .buttons button.edit svg {
  color: #5f7a7c;
  transition: color 0.2s ease;
}

ul.todo li .buttons button.edit:hover svg {
  color: #4a6062;
}

ul.todo li .buttons button.remove svg {
  color: #c0cecb;
  transition: color 0.2s ease;
}

ul.todo li .buttons button.remove:hover svg {
  color: #e85656;
}

ul.todo#completed li {
  opacity: 0.8;
  background-color: #f8f8f8;
}

ul.todo#completed li .text {
  text-decoration: line-through;
  color: #888;
}

.text.completed {
  text-decoration: line-through;
  color: #888;
}

.empty-state {
  text-align: center;
  padding: 30px 20px;
  color: #999;
}

.empty-state p {
  margin-top: 15px;
  font-size: 1.1rem;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  animation: modal-appear 0.3s ease;
}

@keyframes modal-appear {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.modal-header h2 {
  margin: 0;
  color: #5f7a7c;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #666;
}

.modal-body {
  padding: 20px;
}

.detail-section {
  margin-bottom: 20px;
}

.detail-section h3 {
  margin: 0 0 8px 0;
  color: #5f7a7c;
  font-size: 16px;
}

.detail-section p {
  margin: 0;
  padding: 10px;
  background: #f9f9f9;
  border-radius: 6px;
  line-height: 1.5;
}

.no-detail {
  color: #999;
  font-style: italic;
}

.completed {
  color: #25b99a;
  font-weight: 600;
}

.pending {
  color: #e85656;
  font-weight: 600;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  padding: 20px;
  border-top: 1px solid #eee;
  gap: 10px;
}

.edit-btn, .close-modal-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.3s;
}

.edit-btn {
  background: #5f7a7c;
  color: white;
}

.edit-btn:hover {
  background: #4a6062;
}

.close-modal-btn {
  background: #c0cecb;
  color: #333;
}

.close-modal-btn:hover {
  background: #a0b0ad;
}

@media (max-width: 600px) {
  .container {
    border-radius: 12px;
  }
  
  header {
    padding: 15px;
  }
  
  h1 {
    font-size: 1.5rem;
  }
  
  .input-area {
    padding: 15px;
  }
  
  ul.todo {
    padding: 0 15px;
  }
  
  ul.todo li {
    padding: 14px 150px 14px 14px;
  }
  
  ul.todo li .buttons {
    width: 150px;
  }
  
  ul.todo li .buttons button {
    width: 37.5px;
  }
  
  .edit-buttons {
    flex-direction: column;
    margin-left: 5px;
  }
  
  .save-btn, .cancel-btn {
    margin-left: 0;
    margin-bottom: 2px;
    padding: 4px 8px;
    font-size: 10px;
  }
  
  .modal-content {
    width: 95%;
    margin: 10px;
  }
  
  .modal-footer {
    flex-direction: column;
  }
  
  .edit-btn, .close-modal-btn {
    width: 100%;
  }
}
</style>